#include"mr_mem.h"
#include <iostream>
#include <unistd.h>
#include <libmemcached/memcached.h>
#include <mysql.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include   <string.h>
using namespace std;




/*get成功返回 1；失败返回0*/
int mr_memgetp(struct RequInfo Req,struct passwdsend *p){
	 memcached_st *memc;
	 memcached_return rc;
	 memcached_server_st *server;
	 time_t expiration=86400;
     uint32_t  flags;
	 memc = memcached_create(NULL);
         server = memcached_server_list_append(NULL,"211.87.230.80",21201,&rc);
         rc=memcached_server_push(memc,server);
         memcached_server_list_free(server);
         char keya[100]="";
         strcpy(keya,Req.RequInfo);
         char keyb[100]="";
         strcpy(keyb,Req.Param);
         char keyc[100]="";
         strcpy(keyc,Req.Machname);
         char key[300]="";
         strcat(key,keya);
         strcat(key,keyb);
         strcat(key,keyc);
         size_t key_length=sizeof(key);
	     size_t value_length=3200;
         char* result = memcached_get(memc,key,key_length,&value_length,&flags,&rc);
     	if(rc== MEMCACHED_SUCCESS){
     	//rc.substring之类的给struct赋值
     	cout<<"Get value successful!"<<endl;
     	char   *string=result;
	    char   seps[2]=":";
        char   *token;
        char   retoken[4][100];
        //printf(   "Tokens:\n "   );
        token   =   strtok(   string,   seps   );   //   C4996
	   int i=0;
	   while(   token   !=   NULL   )
	   {
		strcpy(retoken[i],token);
		//printf(   "%s\n ",   token   );
		token   =   strtok(   NULL,   seps   );   //   C4996
	    i++;
	   }
        strcpy(p->pw_name,retoken[0]);

        p->pw_uid=atol(retoken[2]);
        strcpy(p->pw_passwd,retoken[1]);
        p->pw_gid=atol(retoken[3]);
        strcpy(p->pw_gecos,p->pw_name);


        strcpy(p->pw_shell,"/bin/bash");
        char temp_hua_7_12[20]="/home/";
	    strcat(temp_hua_7_12,p->pw_name);
        strcpy(p->pw_dir,temp_hua_7_12);
     	return 1;
     	}
     	else {
     	    cout<<"GET data value fail!"<<memcached_strerror(memc,rc)<<endl;
     	    return 0;}
        memcached_free(memc);
}
void mr_memsetp(struct RequInfo Req ,struct passwdsend *p){
         memcached_st *memc;
	     memcached_return rc;
	     memcached_server_st *server;
         time_t expiration=86400;
         uint32_t  flags;
         memc = memcached_create(NULL);
         server = memcached_server_list_append(NULL,"211.87.230.80",21201,&rc);
         rc=memcached_server_push(memc,server);
         memcached_server_list_free(server);
         char keya[100]="";
         strcpy(keya,Req.RequInfo);
         char keyb[100]="";
         strcpy(keyb,Req.Param);
         char keyc[100]="";
         strcpy(keyc,Req.Machname);
         char key[300]="";
         strcat(key,keya);
         strcat(key,keyb);
         strcat(key,keyc);
         char value[400]="";
         char value1[100];
         strcpy(value1,p->pw_name);
         char value2[100];//p->pw_uid;//
         strcpy(value2,p->pw_passwd);

         char value3[100];
         //cout<<(*p).pw_uid<<endl;
         sprintf(value3,"%d",(int)p->pw_uid);
         char value4[100];
         sprintf(value4,"%ld",(long)p->pw_gid);//char value4[100]=p->pw_gid;
         strcat(value,value1);
         strcat(value,":");
         strcat(value,value2);
         strcat(value,":");

         strcat(value,value3);
         strcat(value,":");
         strcat(value,value4);
         strcat(value,":");
         size_t key_length=strlen(key);
         size_t value_length=strlen(value);
         rc=memcached_set(memc,key,key_length,value,value_length,expiration,flags);
     if(rc==MEMCACHED_SUCCESS)
     {
         cout<<"Save data:"<<value<<" sucessful!"<<endl;
     }
      else{
        cout<<"Save data value fail!"<<memcached_strerror(memc,rc)<<endl;
	}
 memcached_free(memc);
}


int mr_memgetsp(struct RequInfo Req,struct spwdsend *sp){
	 memcached_st *memc;
	 memcached_return rc;
	 memcached_server_st *server;
	 time_t expiration=86400;
     uint32_t  flags;
	 memc = memcached_create(NULL);
         server = memcached_server_list_append(NULL,"211.87.230.80",21201,&rc);
         rc=memcached_server_push(memc,server);
         memcached_server_list_free(server);
         char keya[100]="";
         strcpy(keya,Req.RequInfo);
         char keyb[100]="";
         strcpy(keyb,Req.Param);
         char keyc[100]="";
         strcpy(keyc,Req.Machname);
         char key[300]="";
         strcat(key,keya);
         strcat(key,keyb);
         strcat(key,keyc);

         size_t key_length=sizeof(key);
	     size_t value_length=1600;
         char* result = memcached_get(memc,key,key_length,&value_length,&flags,&rc);
     	if(rc== MEMCACHED_SUCCESS){
     	//rc.substring之类的给struct赋值
     	cout<<"Get spvalue successful!"<<endl;
     	char   *string=result;
	    char   seps[2]=":";
        char   *token;
        char   retoken[2][100];
        //printf(   "Tokens:\n "   );
        token   =   strtok(   string,   seps   );   //   C4996
	   int i=0;
	   while(   token   !=   NULL   )
	   {
		strcpy(retoken[i],token);
		//printf(   "%s\n ",   token   );
		token   =   strtok(   NULL,   seps   );   //   C4996
	    i++;
	   }
        strcpy(sp->sp_namp,retoken[0]);


        strcpy(sp->sp_pwdp,retoken[1]);
         sp->sp_lstchg=16528;
                    sp->sp_min=0;
                    sp->sp_max=99999;
                    sp->sp_warn=99999;
                    sp->sp_inact=99999;
                    sp->sp_expire=99999;
                    sp->sp_flag=99999;
        return 1;
     	}
     	else  return 0;

         memcached_free(memc);
}
void mr_memsetsp(struct RequInfo Req ,struct spwdsend *sp){
         memcached_st *memc;
	     memcached_return rc;
	     memcached_server_st *server;
         time_t expiration=86400;
         uint32_t  flags;
         memc = memcached_create(NULL);
         server = memcached_server_list_append(NULL,"211.87.230.80",21201,&rc);
         rc=memcached_server_push(memc,server);
         memcached_server_list_free(server);
         char keya[100]="";
         strcpy(keya,Req.RequInfo);
         char keyb[100]="";
         strcpy(keyb,Req.Param);
         char keyc[100]="";
         strcpy(keyc,Req.Machname);
         char key[300]="";
         strcat(key,keya);
         strcat(key,keyb);
         strcat(key,keyc);
         char value[200]="";
         char value1[100];
         strcpy(value1,sp->sp_namp);
         char value2[100];//p->pw_uid;//
         strcpy(value2,sp->sp_pwdp);
         strcat(value,value1);
         strcat(value,":");
         strcat(value,value2);
         strcat(value,":");
         size_t key_length=strlen(key);
         size_t value_length=strlen(value);
         rc=memcached_set(memc,key,key_length,value,value_length,expiration,flags);
     if(rc==MEMCACHED_SUCCESS)
     {
         cout<<"Save datasp:"<<value<<" sucessful!"<<endl;
     }
      else{
        cout<<"Save datasp value fail!"<<memcached_strerror(memc,rc)<<endl;
	}
 memcached_free(memc);
}






